/*
 * $RCSfile: Room.java,v $
 * $Revision: 1.1  $
 * $Date: 2012-8-10  $
 *
 * Copyright (C) 2005 Bettem, Inc. All rights reserved.
 *
 * This software is the proprietary information of Bettem, Inc.
 * Use is subject to license terms.
 */
package com.dzpk.gameserver.module.main.room;

import java.util.*;

import com.dzpk.gameserver.module.main.RoomThread;
import com.dzpk.gameserver.module.main.cards.Card;
import com.dzpk.gameserver.module.main.pocer.Pocer;
import com.dzpk.gameserver.module.main.pocer.PocerLink;
import org.apache.log4j.Logger;



/**
 * 房间接口
 * <p>Title: Room</p> 
 * <p>Description: </p> 
 * <p>Copyright: Copyright (c) 2006</p> 
 * @author shenxing.ruan
 * @version 1.0
 */
public class Room {
	public static Logger logger = Logger.getLogger(Room.class);
	private Map<Long,RoomPersion> audMap; //当前进入房间的人
	private static Random r = new Random();
	private long roomId; //房间ID
	private int roomPath; //房间路径
	private int status = 1; //1待初始化话 2已经初始化话
	private int roomType = 0; //1 游戏场地 2比赛场 

	private PocerLink pocerLink; //一个房间一副扑克牌

	private int roomStatus; // 1初始等待发牌状态   2倒计时(发每人两张牌) 3第一轮投注(发三张公共牌) 4第二轮投注(发第四张公共牌) 5第三轮投注(发第五张公共牌) 6第四轮投注 7投注完成 分配奖金

	private int currentNumber = -999;//当前操作玩家编号
	private int currentUserStatus = -999;//当前操作玩家状态
	public int manzhuNumber = -999;//小盲注玩家编号
	public int damanzhuNumber = -999;//大盲注玩家编号
	public int zhuangjiaNumber = -999;//庄家编号
	private int maxChouma = 0; //当前桌面最大筹码

	private int manzhu;
	private int damanzhu;
	private int wait = 20; //等待时间20秒
	private int chouma; //需要携带的筹码量 
	private int fuwufei;//服务费

	private int bisaiCount = 0;//比赛场地已经进行的盘数
	private int bisaiStatus = 1;//比赛场地状态 1 准备开始比赛 2 比赛进行中 3 决出了第一名
	private int d1;//第一名奖金
	private int d2;//第二名奖金
	private long zuidajiqrenId;//
	private long t2 = 0;//房间倒计时
	private long t3;//玩家倒计时
	private long sameKey;
//	private DeletePersion deletePersion = new DeletePersion();

	private int jiaZhuCount = 0; //加注的次数
	private final List<Card> sharedCards = new ArrayList<Card>(); //公共牌
	private int actionCount = 0; // 本轮玩家操作次数


	//离开房间人员筹码合计


	/**
	 * 进入房间携带量
	 */
	private int maxgold;

	/**
	 * 最小区间
	 */
	private int min;
	/**
	 * 最大区间
	 */
	private int max;

	private RoomService roomService;

	//房间座位
	private  RoomPersion[] roomPersions = new RoomPersion[8];

	//玩家坐下等待下一盘开始列表
	private RoomPersion[] ddRoomPersions = new RoomPersion[8];

	/**
	 * 房间线程
	 */
	private RoomThread roomThread;

	private Pocer[] pocer = new Pocer[5];

	public void putSharedCard(Card e) {
		sharedCards.add(e);
	}
	public void clearSharedCards() {
		sharedCards.clear();
	}
	public List<Card> getSharedCards() {
		return this.sharedCards;
	}
	/**
	 * 将大小盲注还原
	 *
	 */
	public void clearManzhu() {
//		LeveRoom leveRoom = Cache.getLeveRoom(roomId, roomPath);
//		damanzhu = leveRoom.getDamanzhu();
//		manzhu = leveRoom.getManzhu();
	}
	/**
	 * //房间一级类型 1~5 新手 初级 中级 高级 比赛
	 */
	public int getRoomPath1() {
		return roomPath/10;
	}
	/**
	 * 快场地 慢场
	 * @return
	 */
	public int getRoomPaht2() {
		return roomPath%10;
	}
	public void addActionCount() {
		actionCount ++;
	}

	public void createActionCount() {
		actionCount = 0;
	}
	public int getActionCount() {
		return actionCount;
	}

	public void addJiazhuCount() {
		jiaZhuCount ++;
	}

	public void createJiazhuCount() {
		jiaZhuCount = 0;
	}
	public int getJiaZhuCount() {
		return jiaZhuCount;
	}

	public void anteClean() {
		for (RoomPersion r : roomPersions) {
			if (r != null) {
				r.setAnteNumber(0);
			}
		}
	}

	public void nextGame() {
		this.roomStatus = 1;
		for (RoomPersion r : roomPersions) {
			if (r != null) {
				r.setBetStatus(0);
				r.setBetChouma(0);
				r.setAnteCount(0);
				r.setStatus(0);
				r.setYq(0);
				r.setYs(-1);
			}
		}
//		this.nextmanzhu();
//		roomThread.close();
//		roomThread = null;
//		roomThread = new RoomThread(this);
	}

	/**
	 * 重置 //一盘结束时
	 */
	public void reset() {
		this.clearSharedCards();
		for (int i = 0; i < pocer.length ; i ++) {
			pocer[i] = null;
		}
		for (RoomPersion rp : roomPersions) {
			if (rp != null) {
				rp.clearHoleCards();
			}
		}
		maxChouma = damanzhu;
	}


	public long getSameKey() {
		return sameKey;
	}

	public void setSameKey(long sameKey) {
		this.sameKey = sameKey;
	}

	public long getZuidajiqrenId() {
		return zuidajiqrenId;
	}

	public void setZuidajiqrenId(long zuidajiqrenId) {
		this.zuidajiqrenId = zuidajiqrenId;
	}

	/**
	 * 初始化化房间对象
	 */
	public void initRoom() {
		logger.debug("init room :" + roomId);
		audMap = new HashMap<>(16);
		status = 2;
		roomStatus = 1;
		pocerLink = new PocerLink();
		roomService = RoomService.initRoomService(this);
		if (roomThread != null) {
			roomThread.close(roomId);
		}
		roomThread = new RoomThread(this);

	}

	public long getRoomId() {
		return roomId;
	}

	public int getRoomPath() {
		return roomPath;
	}

	public int getStatus() {
		return status;
	}

	public PocerLink getPocerLink() {
		return pocerLink;
	}


	/**
	 * 获取进入房间人员列表
	 * @return
	 */
	public Map<Long, RoomPersion> getAudMap() {
		return audMap;
	}

	/**
	 * 1初始等待发牌状态   2倒计时(发每人两张牌) 3第一轮投注(发三张公共牌) 4第二轮投注(发第四张公共牌) 5第三轮投注(发第五张公共牌) 6第四轮投注 7投注完成 分配奖金
	 * @return
	 */
	public int getRoomStatus() {
		return roomStatus;
	}

	/**
	 * 当前操作玩家
	 *
	 * @return
	 */
	public int getCurrentNumber() {
		return currentNumber;
	}

	public int getCurrentUserStatus() {
		return currentUserStatus;
	}

	public int getManzhuNumber() {
		return manzhuNumber;
	}

	public int getDamanzhuNumber() {
		return damanzhuNumber;
	}

	public int getZhuangjiaNumber() {
		return zhuangjiaNumber;
	}

	public RoomPersion[] getRoomPersions() {
		return roomPersions;
	}


//	public void setStatus(int status) {
//		this.status = status;
//	}

	/**获取五张底牌
	 * @return
	 */
	public Pocer[] getPocer() {
		return pocer;
	}

	public void setPocer(Pocer[] pocer) {
		this.pocer = pocer;
	}

	/**
	 * 当前牌桌最大筹码
	 * @return
	 */
	public int getMaxChouma() {
		return maxChouma;
	}

	public void setMaxChouma(int maxChouma) {
		this.maxChouma = maxChouma;
	}


	/**
	 * 1 游戏场地 2比赛场 
	 *
	 * @return
	 */
	public int getRoomType() {
		return roomType;
	}


	public void setRoomType(int roomType) {
		this.roomType = roomType;
	}


	public int getManzhu() {
		return manzhu;
	}


	public void setManzhu(int manzhu) {
		this.manzhu = manzhu;
	}


	public int getDamanzhu() {
		return damanzhu;
	}


	public void setDamanzhu(int damanzhu) {
		this.damanzhu = damanzhu;
	}


	public int getWait() {
		return wait;
	}


	public void setWait(int wait) {
		this.wait = wait;
	}

	/**
	 *
	 * 需要携带的筹码量 
	 * @return
	 */
	public int getChouma() {
		return chouma;
	}
	/**
	 *
	 * 需要携带的筹码量 
	 * @param chouma
	 */
	public void setChouma(int chouma) {
		this.chouma = chouma;
	}


	public RoomPersion[] getDdRoomPersions() {
		return ddRoomPersions;
	}


	public void setDdRoomPersions(RoomPersion[] ddRoomPersions) {
		this.ddRoomPersions = ddRoomPersions;
	}


//	public int getDdSize() {
//		return ddSize;
//	}
//
//
//	public void setDdSize(int ddSize) {
//		this.ddSize = ddSize;
//	}

	public int getFuwufei() {
		return fuwufei;
	}

	public void setFuwufei(int fuwufei) {
		this.fuwufei = fuwufei;
	}

	public void setRoomPath(int roomPath) {
		this.roomPath = roomPath;
	}

	/**
	 * 充血模型
	 * 设置房间状态 非常重要 不能乱设置
	 * 1初始等待发牌状态   2倒计时(发每人两张牌) 3第一轮投注(发三张公共牌) 4第二轮投注(发第四张公共牌) 5第三轮投注(发第五张公共牌) 6第四轮投注 7投注完成 分配奖金
	 * @param
	 */
	public void setRoomStatus(int roomStatus) {
		createActionCount();
		createJiazhuCount();
		this.roomStatus = roomStatus;
	}

	public void setRoomId(long roomId) {
		this.roomId = roomId;
	}

	public RoomService getRoomService() {
		return roomService;
	}

	public static Random getR() {
		return r;
	}

	public void setManzhuNumber(int manzhuNumber) {
		this.manzhuNumber = manzhuNumber;
	}

	public void setDamanzhuNumber(int damanzhuNumber) {
		this.damanzhuNumber = damanzhuNumber;
	}

	public void setZhuangjiaNumber(int zhuangjiaNumber) {
		this.zhuangjiaNumber = zhuangjiaNumber;
	}

	public void setCurrentNumber(int currentNumber) {
		this.currentNumber = currentNumber;
	}

	public void setCurrentUserStatus(int currentUserStatus) {
		this.currentUserStatus = currentUserStatus;
	}
	public int getBisaiCount() {
		return bisaiCount;
	}

	public void setBisaiCount(int bisaiCount) {
		this.bisaiCount = bisaiCount;
	}

	/**
	 * 比赛场地状态 1 准备开始比赛 2 比赛进行中 3 决出了第一名
	 *
	 * @return
	 */
	public int getBisaiStatus() {
		return bisaiStatus;
	}

	/**
	 * 比赛场地状态 1 准备开始比赛 2 比赛进行中 3 决出了第一名
	 *
	 * @param bisaiStatus
	 */
	public void setBisaiStatus(int bisaiStatus) {
		this.bisaiStatus = bisaiStatus;
	}

	public RoomThread getRoomThread() {
		return this.roomThread;
	}

	/**
	 * 最大携带量
	 * @return
	 */
	public int getMaxgold() {
		return maxgold;
	}

	/**
	 * 最大携带量
	 * @param maxgold
	 */
	public void setMaxgold(int maxgold) {
		this.maxgold = maxgold;
	}

	public int getMin() {
		return min;
	}

	public void setMin(int min) {
		this.min = min;
	}

	public int getMax() {
		return max;
	}

	public void setMax(int max) {
		this.max = max;
	}

	public int getD1() {
		return d1;
	}

	public void setD1(int d1) {
		this.d1 = d1;
	}

	public int getD2() {
		return d2;
	}

	public void setD2(int d2) {
		this.d2 = d2;
	}

	public int getRoomPersionCount() {
		int count = 0;
		for (RoomPersion roomPersion : this.roomPersions) {
			if (roomPersion != null) {
				count ++;
			}
		}
		return count;
	}
	/**
	 * 正常个数 机器人个数
	 * @return
	 */
	public int getPersoionCount() {
		int persionCount = 0;
		for (RoomPersion roomPersion : this.roomPersions) {
			if (roomPersion != null) {
				persionCount ++;
			}
		}
		return persionCount;
	}
	public double getSlv() {
		if (getRoomType() == 2) {
			return 1;
		}
		int a = 0;
		int b = 0;
		for (RoomPersion roomPersion : this.roomPersions) {
			if (roomPersion != null) {
				a++;
				b++;
			}
		}
		if (b == 0 || a == 0) {
			return 1;
		}
		return b/(a+b);
	}

	public long getT3() {
		return t3;
	}

	public void setT3(long t3) {
		this.t3 = t3;
	}

	public void setT2(long t2) {
		this.t2 = t2;
	}
	public long getT2() {
		return this.t2;
	}
}

